/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dahg.project.istu.web.commons.abstracto;

import com.dahg.project.istu.controller.bean.commons.ICatalogo;
import com.dahg.project.istu.controller.services.IServiceCatalogo;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author dahg
 * @param <T>
 */
public abstract class AbstractConverter<T extends ICatalogo> implements Converter {

    public abstract IServiceCatalogo<T> getService();    
    public Object convertStringToPrimaryKey(String id) {
        return Integer.valueOf(id);
    }
    
    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        Object id = convertStringToPrimaryKey(string);
        return getService().getById(id);
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        T obj = (T) o;
        return obj.getPrimaryKey().toString();
    }
    
}
